package com.project.quiktask.datastore;

import java.sql.Date;
import java.sql.Time;
import java.text.ParseException;
import java.util.Vector;

import com.project.quiktask.controller.DateTimeUtil;
import com.project.quiktask.Task;
import com.project.quiktask.util.Variable;
/**
 * A service class that perform task Object to String conversion and vice versa
 * @author PHAM MINH VIET
 */
public class FileConverter {
		
	
	public static Vector<Task> generateTasks(Vector<String> read) throws ParseException {
		Vector<Task> tasks = new Vector<Task>();
		for (int i = 0; i < read.size(); i++) {
			tasks.add(stringToTaskObject((read.get(i))));
		}
		return tasks;
	}
	
	public static Vector<String> readTaskContent(String taskContent){
		String[] taskFields=taskContent.split("\\|");
		Vector<String> fields=new Vector<String>();
		for(int i=0;i<taskFields.length;i++){
			fields.add(taskFields[i].substring(taskFields[i].indexOf(":")+1));
		}
		return fields;
	}
	
	/**
	 * String Class to task Class converter.
	 * This method is called when stored tasks are loaded from file
	 * @param taskInString
	 * @return task associated with the string
	 * @throws ParseException 
	 */
	
	public static Task stringToTaskObject(String taskInString) throws ParseException {
		Vector<String> taskFields=readTaskContent(taskInString);
		Task newTask=new Task();
		String importance=taskFields.get(0);
		
		if(importance.equals("important"))
			newTask.setImportance(true);
		
		if(!(taskFields.get(1).equals(Variable.NULL)))
			newTask.setTaskDescription(taskFields.get(1));
		
		if(!(taskFields.get(2).equals(Variable.NULL))) {
			Time startTime = DateTimeUtil.tryParseTime(taskFields.get(2));
			newTask.setStartTime(startTime);
		}
		
		if(!(taskFields.get(3).equals(Variable.NULL))) {
			Date startDate = DateTimeUtil.tryParseDate(taskFields.get(3));
			newTask.setStartDate(startDate);
		}
		
		if(!(taskFields.get(4).equals(Variable.NULL))) {
			Time endTime = DateTimeUtil.tryParseTime(taskFields.get(4));
			newTask.setEndTime(endTime);
		}
	
		if(!(taskFields.get(5).equals(Variable.NULL))) {
			Date endDate = DateTimeUtil.tryParseDate(taskFields.get(5));
			newTask.setEndDate(endDate);
		}
		
		if(!(taskFields.get(6).equals(Variable.NULL))) {
			Time deadlineTime= DateTimeUtil.tryParseTime(taskFields.get(6));
			newTask.setDeadlineTime(deadlineTime);
		}
		
		if(!(taskFields.get(7).equals(Variable.NULL))) {
			Date deadlineDate= DateTimeUtil.tryParseDate(taskFields.get(7));
			newTask.setDeadlineDate(deadlineDate);
		}

		return newTask;
	}

	/**
	 * task Class to String Class converter.
	 * This method is called when tasks are written to file
	 * @param task
	 * @return string associated with the converted task
	 */
	public static String taskObjectToString(Task task) {
		String importance;
		String description;
		String startTime;
		String startDate;
		String endTime;
		String endDate;
		String deadlineDate;
		String deadlineTime;
		
		if (task.getImportance())
			importance = "important";
		else
			importance = "not important";

		if(task.getTaskDescription()!=null)
			description=task.getTaskDescription();
		else
			description=Variable.NULL;
		
		if(task.getStartTime()!=null)
			startTime=task.getStartTime().toString();
		else
			startTime=Variable.NULL;
		
		if(task.getStartDate()!=null)
			startDate=task.getStartDate().toString();
		else
			startDate=Variable.NULL;
		
		if(task.getEndTime()!=null)
			endTime=task.getEndTime().toString();
		else
			endTime=Variable.NULL;
		
		if(task.getEndDate()!=null)
			endDate=task.getEndDate().toString();
		else
			endDate=Variable.NULL;
		
		if(task.getDeadlineDate()!=null)
			deadlineDate=task.getDeadlineDate().toString();
		else
			deadlineDate=Variable.NULL;
		
		if(task.getDeadlineTime()!=null)
			deadlineTime=task.getDeadlineTime().toString();
		else
			deadlineTime = Variable.NULL;
		
		return ("importance:"+importance+Variable.FIELD_SEPARATOR
				+"description:"+description+Variable.FIELD_SEPARATOR
				+"start time:"+startTime+Variable.FIELD_SEPARATOR
				+"start date:"+startDate+Variable.FIELD_SEPARATOR
				+"end time:"+endTime+Variable.FIELD_SEPARATOR
				+"end date:"+endDate+Variable.FIELD_SEPARATOR
				+"deadline time:"+deadlineTime+Variable.FIELD_SEPARATOR
				+"deadline date:"+deadlineDate+Variable.NEW_LINE);
	}
}
